import { config } from '../config.js'

const request = (url, data, method, header) => {
  return new Promise((resolve, reject) => {
    wx.showLoading({title: '正在加载中...', mask: true})
    wx.request({
      url: config.api_base_url + url,
      data: data ? data : null,
      method: method ? method : 'GET',
      header: header ? header : { 'content-type': 'application/json' },
      complete: () => {
        wx.hideLoading()
      },
      success: (res) => {
        resolve(res.data)
      },
      fail: (err) => {
        wx.showModal({
          showCancel: false,
          confirmColor: '#1d8f59',
          content: '数据加载失败，请检查您的网络，点击确定重新加载数据!',
          success: function (res) {
            if (res.confirm) {
              apiRequest(url, method, data, header);
            }
          }
        })
      }
    })
  })
}

export default request